package com.zn.listener;

import com.zn.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * 订阅redis topic
 */
@Configuration
public class RedisMessageListener {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //初始化监听器
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                            @Qualifier("updateAgreementPicNotify")
                                            MessageListenerAdapter tradeReportNotify) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        /**监听更新协议列表中的协议图**/
        container.addMessageListener(tradeReportNotify, new PatternTopic(Constant.UPDATE_AGREEMENT_PIC_CHANNEL));
        return container;
    }

    /**
     * 根据CRM推送过来的 agreementId 进行客户图片更改
     * @param redisReceiver
     * @return
     */
    @Bean
    @Primary
    MessageListenerAdapter updateAgreementPicNotify(RedisReceiver redisReceiver) {
        return new MessageListenerAdapter(redisReceiver, "updateAgreementPicNotify");
    }
}